﻿using System;
using DotNetNuke.UI.ControlPanels;

namespace BrandonHaynes.Providers.ControlPanel
    {
    /// <summary>
    /// The DotNetNuke ControlPanelBase class allows only a instantiation
    /// to be associated with a request (see ControlPanelBase.Init).
    /// This causes a problem when we begin nesting control panels,
    /// where only one topmost control exists, but that control contains
    /// other ControlPanelBase instances.
    /// 
    /// To rememdy this issue, in the case where we know we will be 
    /// nesting control panels we work around this singleton-per-request
    /// limitation via hack.
    /// </summary>
    public class NestedControlPanelBase : ControlPanelBase
        {
        /// <summary>
        /// This class is way to messy to allow instantiation by the outside world.
        /// It is possible that its implementation will need to change with subsequent
        /// versions of DotNetNuke.
        /// </summary>
        internal NestedControlPanelBase() {}

        protected override void OnInit(EventArgs e)
            {
            // Apply our hack before and after initialization, just in case.
            AllowEmbeddedControlPanel();
            base.OnInit(e);
            AllowEmbeddedControlPanel();
            }

        /// <summary>
        /// An ugly horrible hack that allows multiple ControlPanelBase 
        /// initializations on a single request.  Use with caution.
        /// </summary>
        public void AllowEmbeddedControlPanel()
            { Page.Items[typeof(ControlPanelBase)] = null; }
        }
    }